import fetch from './fetch';
import logger from '../../utils/logger';
import { Category } from '../../types';

/**
 * 请求项目api列表
 */
const getProjectExport = (serverUrl: string, token: string) => {
  try {
    const url = `${serverUrl}/api/plugin/export`;
    return fetch.get<Category[]>({
      url,
      params: {
        type: 'json',
        status: 'all',
        isWiki: 'false',
        token
      }
    });
  } catch (err) {
    logger.error(err);
    process.exit(1);
  }
};

/**
 * 获取项目信息
 * @param serverUrl
 * @param token
 */
const getProjectInfo = (serverUrl: string, token: string) => {
  try {
    const url = `${serverUrl}/api/project/get`;
    return fetch.get({
      url,
      params: {
        token
      }
    });
  } catch (error) {
    logger.error(error);
    process.exit(1);
  }
};

/**
 * 获取api菜单分类
 * @param serverUrl
 * @param token
 * @param projectId
 */
const getCatMenu = (serverUrl: string, token: string, projectId: string) => {
  try {
    const url = `${serverUrl}/api/interface/getCatMenu`;
    return fetch.get({
      url,
      params: {
        token,
        project_id: projectId
      }
    });
  } catch (error) {
    logger.error(error);
    process.exit(1);
  }
};

export { getProjectExport, getProjectInfo, getCatMenu };
